package com.bj58.disconf.web.service.app;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.bj58.disconf.web.common.utils.DBHelper;
import com.bj58.disconf.web.service.app.bo.App;
import com.bj58.disconf.web.service.app.dao.AppDao;

/**
 * @author lemon
 * @version 2014-6-16
 */
@Service
public class AppDaoImpl implements AppDao {

    public Long insert(App app) {
        try {
            Object id = DBHelper.getInstance().getDao().sql.insert(app);
            if (id != null) {
                return (Long) id;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public void delete(long appid) {
        try {
            DBHelper.getInstance().getDao().sql.deleteByID(App.class, appid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public App getByName(String name) {
        try {
            List<App> ulist = DBHelper.getInstance().getDao().sql.getListByCustom(App.class, "*", "name=" + name, "");
            return ulist != null ? ulist.get(0) : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public App getByID(long id) {
        try {
            List<App> ulist = DBHelper.getInstance().getDao().sql.getListByIDS(App.class, new Object[] { id });
            return ulist != null ? ulist.get(0) : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<App> getByIds(Set<Long> ids) {
        try {
            if (ids == null) {
                return null;
            }
            List<App> ulist = DBHelper.getInstance().getDao().sql.getListByIDS(App.class, ids.toArray());
            return ulist;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<App> findAll() {
        try {
            List<App> ulist = DBHelper.getInstance().getDao().sql.getListByCustom(App.class, "*", "", "");
            return ulist;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
